//
//  MessageManager.m
//  Showing
//
//  Created by Ralf on 4/9/15.
//  Copyright (c) 2015 next-software. All rights reserved.
//

@import UIKit;

#import "MessageManager.h"

@implementation MessageManager

- (ResultSet *)queryContacts
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];

    return [self request:@"message/queryContacts" params:params];
}

- (ResultSet *)receiveMessage:(NSString *)userName
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:userName forKey:@"userName"];
    [params setValue:@"100" forKey:@"max"];
    [params setValue:@"0" forKey:@"offset"];

    return [self request:@"message/receiveMessage" params:params];
}

- (ResultSet *)sendMessage:(NSDictionary *)message
{
    NSDictionary *params = [NSMutableDictionary new];

    [params setValue:[self getSessionID] forKey:@"sessionID"];
    [params setValue:message[@"userName"] forKey:@"userName"];
    [params setValue:message[@"message"] forKey:@"message"];

    NSString *messageType = message[@"messageType"];
    [params setValue:messageType forKey:@"messageType"];
    if (messageType && [messageType isEqualToString:@"Audio"])
    {
        NSData   *audio  = message[@"audio"];
        NSData   *data   = [audio base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:data.length encoding:NSUTF8StringEncoding];
        [params setValue:string forKey:@"audio"];

    }
    else if (messageType && [messageType isEqualToString:@"Image"])
    {
        NSData   *image  = UIImageJPEGRepresentation(message[@"image"], 0.6);
        NSData   *data   = [image base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:data.length encoding:NSUTF8StringEncoding];
        [params setValue:string forKey:@"image"];
    }

    return [self requestJSON:@"message/sendMessage" params:params];
}

@end
